package cn.akwangl.design.pattern.creational.simplefactory;

/**
 * 简单工厂demo.
 *
 * @author youyongkun
 * @date 2021/11/23 14:44
 * @since 1.0
 */
public class VideoFactory {

    /**
     * 根据传入字符串获取相关{@link AbstractVideoFactory}实现,若无匹配则返回null
     *
     * @param videoName 视频名称
     * @return {@link AbstractVideoFactory}实现
     * @author youyongkun
     * @date 2021/11/23 14:44
     * @since 1.0
     */
    public static AbstractVideoFactory getVideo(String videoName) {
        if (JavaVideo.VIDEO_NAME.equalsIgnoreCase(videoName)) {
            return new JavaVideo();
        } else if (PythonVideo.VIDEO_NAME.equalsIgnoreCase(videoName)) {
            return new PythonVideo();
        }
        return null;
    }

    /**
     * 根据传入{@link Class} 类型获取相关{@link AbstractVideoFactory}实现,若无匹配则返回null
     *
     * @param c {@link AbstractVideoFactory} 实现 class
     * @return {@link AbstractVideoFactory}实现
     * @author youyongkun
     * @date 2021/11/23 14:44
     * @since 1.0
     */
    public static <T extends AbstractVideoFactory> T getVideo(Class c) {
        try {
            return (T) Class.forName(c.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
